const http  = require('http');

function request(url) {
    return new Promise((resolve, reject) => {
        http.get(url, incomming => {
            if (incomming.statusCode === 200) {
                incomming.setEncoding('utf-8');
                let rawData = '';
                incomming.on('data', chunk => {
                    rawData += chunk;
                });
                incomming.on('end', () => {
                    resolve(rawData);
                    // callback(rawData);
                    // console.log(rawData);
                    // return rawData;
                });
            } else {
                // console.log(incomming.statusMessage);
                reject(incomming.statusMessage);
            }
        });
    });
}

// function request(url, callback) {
//     url = 'http://v3.wufazhuce.com:8000' + url;
//     http.get(url, incomming => {
//         if (incomming.statusCode === 200) {
//             incomming.setEncoding('utf-8');
//             let rawData = '';
//             incomming.on('data', chunk => {
//                 rawData += chunk;
//             });
//             incomming.on('end', () => {
//                 callback(rawData);
//                 // console.log(rawData);
//                 // return rawData;
//             });
//         } else {
//             console.log(incomming.statusMessage);
//         }
//     });
// }

// function request(url) {
//     url = 'http://v3.wufazhuce.com:8000' + url;
//     http.get(url, incomming => {
//         if (incomming.statusCode === 200) {
//             incomming.setEncoding('utf-8');
//             let rawData = '';
//             incomming.on('data', chunk => {
//                 rawData += chunk;
//             });
//             incomming.on('end', () => {
//                 console.log(rawData);
//                 // return rawData;
//             });
//         } else {
//             console.log(incomming.statusMessage);
//         }
//     });
// }

module.exports  = {
    request
};
